﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>你的函数使用对了吗?-------C语言函数学习(3)详解</title>
  <link rel="stylesheet" href="https://stackedit.io/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><blockquote>
<p>这篇文章是学习函数的第三集,小伙伴们,快来看看今天我们要学习什么吧!💕💕</p>
</blockquote>
<p></p><div class="toc"><h3>目录</h3><ul><li><a href="#_16">一、函数的嵌套调用和链式访问</a></li><ul><li><a href="#_19">函数嵌套调用(正确)</a></li><li><a href="#_41">函数嵌套定义(错误)</a></li><li><a href="#_57">链式访问</a></li><ul><li><a href="#4321_73">关于链式访问的例子----你知道为什么运行结果是4321吗?😉</a></li></ul></ul><li><a href="#_94">二、函数的声明和定义</a></li><ul><li><a href="#_95">注意：(对比函数和变量来看)</a></li><ul><li><a href="#1_105">1.定义变量时如果非要在后面定义,前面使用,编译器不认识,那么就必须在使用之前声明变量</a></li><li><a href="#2_addc_128">2. 调用add函数将两数相乘的值给c</a></li><ul><li><a href="#_129">错误示范</a></li><li><a href="#__149">** 解决办法**:在调用之前声明函数</a></li><li><a href="#_184">在工程中应该怎么写?---分模块写</a></li><ul><li><a href="#_204">分模块的思想是什么样子的?</a></li></ul></ul></ul></ul></ul></div><p></p>
<hr>
<hr>
<p><code>提示：以下是本篇文章正文内容，下面案例可供参考</code></p>
<h1><a id="_16"></a>一、函数的嵌套调用和链式访问</h1>
<p>函数和函数之间可以根据实际的需求进行组合的，也就是互相调用的。<br>
函数可以嵌套调用，但是不能嵌套定义。</p>
<h2><a id="_19"></a>函数嵌套调用(正确)</h2>
<pre><code class="prism language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">void</span> <span class="token function">new_line</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"hehe\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">void</span> <span class="token function">three_line</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
 <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">3</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
   <span class="token punctuation">{</span>
        <span class="token function">new_line</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
 <span class="token function">three_line</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2><a id="_41"></a>函数嵌套定义(错误)</h2>
<pre><code class="prism language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_CRT_SECURE_NO_WARNINGS</span> <span class="token expression"><span class="token number">1</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">void</span> <span class="token function">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">void</span> <span class="token function">B</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

	<span class="token punctuation">}</span>
 <span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre>
<h2><a id="_57"></a>链式访问</h2>
<p>把一个函数的返回值作为另外一个函数的参数</p>
<pre><code class="prism language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string.h&gt;</span> <span class="token comment">//strlen的头文件</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token comment">//一般求字符串的长度写法:</span>
	<span class="token comment">//int len = strlen("abcdef");</span>
	<span class="token comment">//printf("%d\n", len);</span>

	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span><span class="token string">"abcdef"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//链式访问  strlen函数调用的返回值作为printf函数的参数 </span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/a3b9843ad5684e938d721d3654b98c9a.png" alt="在这里插入图片描述"></p>
<h3><a id="4321_73"></a>关于链式访问的例子----你知道为什么运行结果是4321吗?😉</h3>
<pre><code class="prism language-c"> <span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token number">43</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//通过观察注意到打印的时候要先执行printf("%d", 43),而这个printf的返回值是什么呢?</span>
<span class="token comment">//查找printf的相关定义文档可知:它的返回值是总共被打印的字符的个数.</span>
<span class="token comment">//printf("%d",43)     打印43   4是一个字符,3是1个字符,总共被打印的字符的个数是2,所以这个printf的返回值是2</span>
<span class="token comment">//接下来由内到外的第二层函数变为:printf("%d",2)  打印2   2是一个字符,这个函数的返回值是1</span>
<span class="token comment">//最后变为printf("%d",1)  打印1</span>
<span class="token comment">//所以运行结果是4321</span>

<span class="token comment">//另外需要注意的是:如果%d后面有一个空格,这时与上面的运行结果就会截然不同</span>
<span class="token comment">//例如:printf("%d ",43); 在屏幕上打印43,但是4,3,空格各占一个字符,总共被打印的字符的个数是3,这个函数的返回值是3</span>

</code></pre>
<p><img src="https://img-blog.csdnimg.cn/ad45a275bf5a42a3b268e486a62848ae.png" alt="在这里插入图片描述"></p>
<h1><a id="_94"></a>二、函数的声明和定义</h1>
<h2><a id="_95"></a>注意：(对比函数和变量来看)</h2>
<p>变量有声明和定义<br>
函数也有声明和定义<br>
<strong>函数声明：</strong></p>
<ol>
<li>告诉编译器有一个函数叫什么，参数是什么，返回类型是什么。但是具体是不是存在，函数<br>
声明决定不了。</li>
<li>函数的声明一般出现在函数的使用之前。要满足先声明后使用。</li>
<li>函数的声明一般要放在头文件中的。<br>
<strong>函数定义：</strong><br>
函数的定义是指函数的具体实现，交待函数的功能实现</li>
</ol>
<h3><a id="1_105"></a>1.定义变量时如果非要在后面定义,前面使用,编译器不认识,那么就必须在使用之前声明变量</h3>
<pre><code class="prism language-c"><span class="token keyword">int</span> a <span class="token punctuation">;</span>   <span class="token comment">//变量的声明</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>   <span class="token comment">//变量的定义</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/bf2ec1e2baa8471fb9b96c04d0bb646e.png" alt="在这里插入图片描述"></p>
<pre><code class="prism language-c"><span class="token keyword">int</span> a<span class="token punctuation">;</span>   <span class="token comment">//定义 因为后面没有int a 所以这句是定义,且全局变量不初始化是0</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//所以不管是变量还是函数,都得满足先定义后使用  或者  先声明后使用</span>
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/7aa8b216d3ba468d931738f523b21e87.png" alt="在这里插入图片描述"></p>
<h3><a id="2_addc_128"></a>2. 调用add函数将两数相乘的值给c</h3>
<h4><a id="_129"></a>错误示范</h4>
<pre><code class="prism language-c"><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
		<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> c <span class="token operator">=</span> <span class="token function">Add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
	<span class="token keyword">int</span> <span class="token function">Add</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> x <span class="token operator">*</span> y<span class="token punctuation">;</span>

	<span class="token punctuation">}</span>
</code></pre>
<p><strong>报错:</strong><br>
<img src="https://img-blog.csdnimg.cn/1c9247566bf54bd59383a11c6b4acbce.png" alt="在这里插入图片描述"></p>
<h4><a id="__149"></a>** 解决办法**:在调用之前声明函数</h4>
<pre><code class="prism language-c"> <span class="token keyword">int</span> <span class="token function">Add</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//声明函数(必须出现在调用Add函数之前,可以放在main内部或者外部)</span>
                           <span class="token comment">//也可以写成int Add(int,int);</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> c <span class="token operator">=</span> <span class="token function">Add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">Add</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>     <span class="token comment">//函数定义</span>
		<span class="token keyword">return</span> x <span class="token operator">*</span> y<span class="token punctuation">;</span>

	<span class="token punctuation">}</span>
</code></pre>
<p><strong>但是最好的写法如下:</strong></p>
<pre><code class="prism language-c"><span class="token keyword">int</span> <span class="token function">Add</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span>     <span class="token comment">//函数定义并声明</span>
	<span class="token keyword">return</span> x <span class="token operator">*</span> y<span class="token punctuation">;</span>

<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> c <span class="token operator">=</span> <span class="token function">Add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h4><a id="_184"></a>在工程中应该怎么写?—分模块写</h4>
<p>(1)创建一个头文件add.h,一个源文件add.c<br>
(2)在头文件中进行函数的声明,在源文件中进行函数的定义</p>
<pre><code class="prism language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">"Add.h"</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> c <span class="token operator">=</span> <span class="token function">Add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%d\n"</span><span class="token punctuation">,</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Add.c<br>
<img src="https://img-blog.csdnimg.cn/a2a51e6f29a643f98f5c4ea878fc826a.png" alt="在这里插入图片描述"><br>
Add.h<br>
<img src="https://img-blog.csdnimg.cn/df5b99536483454a9efdb8d066b07386.png" alt="在这里插入图片描述"><br>
运行结果:<br>
<img src="https://img-blog.csdnimg.cn/400148e0e64441549f937fd096170d7a.png" alt="在这里插入图片描述"></p>
<h5><a id="_204"></a>分模块的思想是什么样子的?</h5>
<p>通过画图来看:</p>
<p><img src="https://img-blog.csdnimg.cn/f296b27973214e6eae62de0dbcd16881.png" alt="在这里插入图片描述"></p>
<p>你学会了吗小伙伴们💕💕💕<br>
记得点赞收藏么么哒~😘😘</p>
</div>
</body>

</html>
